﻿Friend Class Handler_MediaInfo
    Public Shared Function GetGenresInfo(ByVal mediaInfo As Entities.MediaInfo) As String
        If mediaInfo IsNot Nothing Then
            Return Framework.Serialization.ToCommaSepString(mediaInfo.Genres)
        Else
            Return String.Empty
        End If
    End Function
    Public Shared Function GetFeatureIds(ByVal mediaInfo As Entities.MediaInfo) As Generic.List(Of String)
        If mediaInfo IsNot Nothing Then
            If mediaInfo.OpenMediaInfo IsNot Nothing Then
                If mediaInfo.OpenMediaInfo.Feature IsNot Nothing Then
                    Return mediaInfo.OpenMediaInfo.Feature.Ids
                End If
            End If
        End If
        Return New Generic.List(Of String)
    End Function
    Public Shared Function GetRuntimeInfo(ByVal mediaInfo As Entities.MediaInfo) As String
        Dim clsSB As New System.Text.StringBuilder
        If mediaInfo IsNot Nothing Then
            If mediaInfo.OpenMediaInfo IsNot Nothing Then
                If mediaInfo.OpenMediaInfo.Streams IsNot Nothing Then
                    For Each clsS As Entities.OpenMediaInfo.StreamInfo In mediaInfo.OpenMediaInfo.Streams
                        If clsSB.Length > 0 Then clsSB.Append(","c)
                        If clsS.Runtime IsNot Nothing Then
                            clsSB.Append(clsS.Runtime.Trim)
                        Else
                            clsSB.Append("?"c)
                        End If
                    Next
                End If
            End If
        End If
        Return clsSB.ToString
    End Function
    Public Shared Function GetVideoInfo(ByVal mediaInfo As Entities.MediaInfo) As String
        Dim clsSB As New System.Text.StringBuilder
        For Each clsVS As Entities.OpenMediaInfo.VideoStreamInfo In GetVideoStreams(mediaInfo)
            If clsSB.Length > 0 Then clsSB.Append(", ")
            clsSB.Append(String.Format("{4} {0}x{1} {3} ({2})", clsVS.Width, clsVS.Height, clsVS.Bitrate, clsVS.Framerate, clsVS.Format))
        Next
        Return clsSB.ToString
    End Function
    Public Shared Function GetAudioInfo(ByVal mediaInfo As Entities.MediaInfo) As String
        Dim clsSB As New System.Text.StringBuilder
        For Each clsAS As Entities.OpenMediaInfo.AudioStreamInfo In GetAudioStreams(mediaInfo)
            If clsSB.Length > 0 Then clsSB.Append(", ")
            clsSB.Append(String.Format("{3} {0} {1} ({2})", clsAS.Language.Trim, clsAS.ChannelPositions.Trim, clsAS.Bitrate, clsAS.Format))
        Next
        Return clsSB.ToString
    End Function
    Public Shared Function GetSubtitlesInfo(ByVal mediaInfo As Entities.MediaInfo) As String
        Dim clsSB As New System.Text.StringBuilder
        For Each clsAS As Entities.OpenMediaInfo.SubtitleStreamInfo In GetSubtitleStreams(mediaInfo)
            If clsSB.Length > 0 Then clsSB.Append(", ")
            clsSB.Append(clsAS.Language.Trim)
        Next
        Return clsSB.ToString
    End Function
    Public Shared Function GetYear(ByVal mediaInfo As Entities.MediaInfo) As String
        If mediaInfo IsNot Nothing Then
            If mediaInfo.OpenMediaInfo IsNot Nothing Then
                If mediaInfo.OpenMediaInfo.Feature IsNot Nothing Then
                    Return mediaInfo.OpenMediaInfo.Feature.Year
                End If
            End If
        End If
        Return String.Empty
    End Function
    Public Shared Function GetFeatureTitle(ByVal mediaInfo As Entities.MediaInfo, ByVal [default] As String) As String
        If mediaInfo IsNot Nothing Then
            If Not String.IsNullOrEmpty(mediaInfo.FeatureTitle) Then
                Return mediaInfo.FeatureTitle
            End If
        End If
        Return [default]
    End Function
    Private Shared Function GetVideoStreams(ByVal mediaInfo As Entities.MediaInfo) As Generic.List(Of Entities.OpenMediaInfo.VideoStreamInfo)
        Dim aRet As New Generic.List(Of Entities.OpenMediaInfo.VideoStreamInfo)
        If mediaInfo IsNot Nothing Then
            If mediaInfo.OpenMediaInfo IsNot Nothing Then
                If mediaInfo.OpenMediaInfo.Streams IsNot Nothing Then
                    For Each clsS As Entities.OpenMediaInfo.StreamInfo In mediaInfo.OpenMediaInfo.Streams
                        aRet.AddRange(clsS.VideoStreams)
                    Next
                End If
            End If
        End If
        Return aRet
    End Function
    Private Shared Function GetAudioStreams(ByVal mediaInfo As Entities.MediaInfo) As Generic.List(Of Entities.OpenMediaInfo.AudioStreamInfo)
        Dim aRet As New Generic.List(Of Entities.OpenMediaInfo.AudioStreamInfo)
        If mediaInfo IsNot Nothing Then
            If mediaInfo.OpenMediaInfo IsNot Nothing Then
                If mediaInfo.OpenMediaInfo.Streams IsNot Nothing Then
                    For Each clsS As Entities.OpenMediaInfo.StreamInfo In mediaInfo.OpenMediaInfo.Streams
                        aRet.AddRange(clsS.AudioStreams)
                    Next
                End If
            End If
        End If
        Return aRet
    End Function
    Private Shared Function GetSubtitleStreams(ByVal mediaInfo As Entities.MediaInfo) As Generic.List(Of Entities.OpenMediaInfo.SubtitleStreamInfo)
        Dim aRet As New Generic.List(Of Entities.OpenMediaInfo.SubtitleStreamInfo)
        If mediaInfo IsNot Nothing Then
            If mediaInfo.OpenMediaInfo IsNot Nothing Then
                If mediaInfo.OpenMediaInfo.Streams IsNot Nothing Then
                    For Each clsS As Entities.OpenMediaInfo.StreamInfo In mediaInfo.OpenMediaInfo.Streams
                        aRet.AddRange(clsS.SubtitleStreams)
                    Next
                End If
            End If
        End If
        Return aRet
    End Function
End Class
